package com.zhkj.txg.module.mine.ui

import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.lib.utils.AccountManager
import com.zhkj.lib.utils.SpanUtil
import com.zhkj.txg.R
import com.zhkj.txg.module.mine.adapter.BalanceAdapter
import com.zhkj.txg.module.mine.vm.BalanceViewModel
import kotlinx.android.synthetic.main.activity_mine_balance.*

class MineBalanceActivity : ViewModelActivity<BalanceViewModel>() {

    companion object {
        fun start(activity: Activity) {
            val intent = Intent(activity, MineBalanceActivity::class.java)
            activity.startActivity(intent)
        }
    }

    override fun setViewModel(): BalanceViewModel = createViewModel()
    override fun statusTitleBar(): View? = titleMineBalance
    override val layoutResId: Int = R.layout.activity_mine_balance

    private var refresh = false
    private val detailAdapter = BalanceAdapter().apply {
        loadMoreModule?.isEnableLoadMore = true
        loadMoreModule?.setOnLoadMoreListener {
            viewModel.balanceList(AccountManager.getToken())
        }
    }

    override fun initUI() {
        tvBalanceRecharge.setOnClickListener {
            BalanceOperationActivity.startRecharge(this)
        }
        tvBalanceWithdraw.setOnClickListener {
            BalanceOperationActivity.startWithdraw(this)
        }
        with(rvBalance) {
            layoutManager = LinearLayoutManager(this@MineBalanceActivity)
            adapter = detailAdapter
        }
        detailAdapter.setEmptyView(R.layout.layout_empty_data)
        refreshBalance.setOnRefreshListener { initData() }
    }

    override fun initData() {
        viewModel.refreshBalance(true)
    }

    override fun onResume() {
        super.onResume()
        if (refresh) {
            initData()
            refresh = false
        }
    }

    override fun initViewModel() {
        // 余额刷新通知观察者
        viewModel.balanceChangeSubscribe().observe(this, Observer { refresh = true })
        // 余额数据观察者
        viewModel.balanceLiveData.observe(this, Observer { response ->
            handlerResponseStatus(response.status, response.statusTip)
            if (response.status == HttpResponseStatus.Status.SUCCESS)
                response.response?.data?.userInfo?.let {
                    tvBalanceAmount.text = SpanUtil.setMoneySymbol(it.money)
                }
        })
        // 余额日志数据观察者
        viewModel.balanceListLiveData.observe(this, Observer { response ->
            when (response.status) {
                HttpResponseStatus.Status.START -> Unit
                HttpResponseStatus.Status.SUCCESS -> response.response?.let {
                    // 判断加载状态
                    if (!it.loadMore) refreshBalance.finishRefresh()
                    else if (it.hasMore) detailAdapter.loadMoreModule?.loadMoreComplete()
                    else detailAdapter.loadMoreModule?.loadMoreEnd()
                    // 数据填充
                    if (it.loadMore) detailAdapter.addData(it.data)
                    else detailAdapter.setNewData(it.data)
                }
                HttpResponseStatus.Status.FAILURE -> {
                    response.exception?.printStackTrace()
                    toast(response.statusTip)
                    if (viewModel.page > 1) detailAdapter.loadMoreModule?.loadMoreFail()
                    else refreshBalance.finishRefresh(false)
                }
            }
        })
    }
}